#include <iostream>
#include <list>
#include<chrono>

/**
Total 
10000
num 
100
out : 9332 time 21

Total 
100000
num 
100
out : 3365 time 209

Total 
1000000
num 
100 
out : 138899 time 3246

*/
using namespace std;

list<int> _list;

//初始化链表
void initList(int count){
    
    for(int i=1;i<=count;++i){
        _list.push_back(i);
    }
}
int fire(int num){
    
    auto _node = _list.begin();
    do{
        for(int i = 1 ; i < num ;++i){
            _node++;
            if(_node ==_list.end()){
                _node = _list.begin();
            }
        }
        _node = _list.erase(_node);
        if(_node ==_list.end()){
                _node = _list.begin();
            }
    }while( _list.size() > 1);

    auto iter = _list.begin();
    return *iter;
}
int getTime(){
    auto time_now = chrono::system_clock::now();
    auto duration_in_ms = chrono::duration_cast<chrono::milliseconds>(time_now.time_since_epoch());
    return duration_in_ms.count();
}
int main(int argc, char* argv[]){
    std::cout<< "Total "<< std::endl;
    int count = 0;
    std::cin >> count;
    std::cout << "num "<< std::endl;
    int num = 0;
    std::cin >> num ;
    initList(count);
    int btime = getTime();
    int out = fire(num);
    int etime = getTime();
    std::cout << "out : "<< out << " time "<< etime - btime<<std::endl;
    return 0;
}
